package com.example.demo.otherController;

import com.example.demo.util.ZipFileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.zip.ZipOutputStream;

/**
 * @author 谢辉
 * @Classname ZipTest
 * @Description TODO
 * @Date 2021/1/6 10:30
 */
@Controller
public class ZipTest {
    @RequestMapping("/test")
    public void exportShowPic(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String zipName = "myfile.zip";
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try {
            ZipFileUtil.doCompress("D:\\file", out);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
}
